package com.order.controller.seller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.order.interceptor.AuthInterceptor;
import com.order.pojo.OrderDetail;
import com.order.pojo.OrderMaster;
import com.order.service.IOrderDetailService;
import com.order.service.IOrderMasterService;
import com.order.utils.Home;
import com.order.vo.ResultVo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.List;

@RestController
@RequestMapping("/statistics")
@Slf4j
public class HomeController {
    @Autowired
    IOrderMasterService iOrderMasterService;
    @Autowired
    IOrderDetailService iOrderDetailService;

    @GetMapping("/overview")
    public ResultVo<Home> overview(){
        Home home=new Home();
       List<OrderMaster> orderMaster = iOrderMasterService.getSum();
        if (orderMaster == null) {
            orderMaster = new ArrayList<>();  // 赋值为空集合，避免 null
        }
         home.setTodayOrders(orderMaster.size());
         home.setActiveUsers(orderMaster.size());
         int sum=0;
        BigDecimal money = BigDecimal.ZERO;  // 正确初始化
        for (OrderMaster master : orderMaster) {
            List<OrderDetail> list = iOrderDetailService.list(
                    new QueryWrapper<OrderDetail>().eq("order_id", master.getOrderId())
            );
            log.info(list.toString());
            money = money.add(master.getOrderAmount());
            sum += list.size();
        }
        home.setTotalProducts(sum);
        home.setTotalAmount(money);
        log.info(new ResultVo<>(0,"查询成功",home).toString());
        return new ResultVo<>(0,"查询成功",home);
    }
}
